package com.gt.web.srv;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * <p>
 * PumaApplication
 * </p>
 *
 * @author: WeiHuan
 * @version:1.0
 * @date: 2024/1/10 11:49
 * 注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目的
 */

@SpringBootApplication(scanBasePackages={"com.gt.web.*"})
@MapperScan(basePackages = "com.gt.web.*.mapper")
@ServletComponentScan(basePackages = "com.web.web.*")
public class GTApplication {

    public static void main(String[] args) {
        SpringApplication.run(GTApplication.class, args);
    }
    /**
     * 设置 redisTemplate 的序列化设置
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //1.创建redisTemplate模板
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //2.关联redisConnectionFactory
        template.setConnectionFactory(redisConnectionFactory);
        //3.创建序列化类
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        //4.设置可见度
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //5.启动默认的类型
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        //6.序列化类，对象映射设置
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
       // 7.设置value的转化格式和key的转换格式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}